package hot150;

// https://leetcode.cn/problems/best-time-to-buy-and-sell-stock/?envType=study-plan-v2&envId=top-interview-150
public class q7 {

    public int maxProfit(int[] prices) {
        return method1(prices, prices.length);
    }

    /**
     * 贪心
     * 1 ms
     * 复杂度O(n)
     */
    public int method1(int[] prices, int n) {
        int maxProfit = 0;
        int mn = prices[0];
        for (int price : prices) {
            // 找到最小值
            mn = Math.min(mn, price);
            // 与当前值比较，求出最大利润
            maxProfit = Math.max(maxProfit, price - mn);
        }
        return maxProfit;
    }
}
